<template>
  <div class="app-container">
    <el-form v-if="isCustom" :model="listQuery" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="组合代码" prop="fundIds">
        <el-select v-model="listQuery.fundIds"
                   placeholder="请选择组合代码"
                   clearable
                   filterable
                   multiple
                   size="small"
                   style="width: 240px">
          <el-option v-for="item in fundList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="估值日期">
        <el-date-picker
          v-model="listQuery.valDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan"  icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
      </el-form-item>
    </el-form>
      <el-tabs v-if="isCustom"
        v-model="fundTabsValue"
        type="border-card"
        closable
        @tab-remove="removeTab"
      >
        <el-tab-pane
          v-for="(item, index) in fundTabs"
          :key="index"
          :label="item.title"
          :name="item.name"
        >
          <ChartItem ref="chartInfo" :resId="resId" :paramsInfo="item.content" :key="timer"/>
        </el-tab-pane>
      </el-tabs>
    <ChartItem v-if="!isCustom" ref="chartInfo" :resId="resId" :key="timer"/>
  </div>
</template>

<script>
import ChartItem from "./chartItem";
import { getMenuByPath } from "@/api/system/menu";
export default {
  name: "Rpchart",
  components: {ChartItem},
  data() {
    return {
      listQuery: {
        fundIds: [],
        valDate:'',
      },
      timer: '1',
      showSearch: true,
      fundTabsValue: "2",
      isCustom: true,
      resId:'',
      fundList:[
        {
          name: "U24600_安鑫稳健7号",
          id: "U24600",
          content: ''
        },
        {
          name: "U24700_安鑫稳健8号",
          id: "U24700",
          content: ''
        },
        {
          name: "U24800_安鑫稳健9号",
          id: "U24800",
          content: ''
        },
      ],
      fundTabs: [
      ],
      tabIndex: 1,
      formLabelWidth: "120px",
    };
  },
  beforeCreate(){
  },
  watch: {
    $route() {
      if (this.$route.params.id)
      {
        //console.log(this.$route.params.id)
        //console.log(this.$store.state.tagsView.cachedViews)
        getMenuByPath(this.$route.params.id).then(response => {
          this.$route.meta.title = response.msg === null ? '报表' : response.msg;
          this.$store.commit('tagsView/EDIT_VISITED_VIEWS', this.$route);
        });
      }
    },
    'this.$route.params.id': {
      immediate: true,
      handler() {
        this.resId = this.$route.params.id;
        getMenuByPath(this.resId).then(response => {
          this.$route.meta.title = response.msg === null ? '报表' : response.msg;
          this.$store.commit('tagsView/EDIT_VISITED_VIEWS', this.$route);
        });
        this.isCustom = this.resId === 'I2c96074701679d2d9d2dba2f0167a55866132c17';
        this.timer = new Date().getTime();
      }
    }
  },

  methods: {
    fetchData() {
      this.fundTabs = [];
      this.listQuery.fundIds.forEach((tab, index) => {
        this.fundList.find((item) => {
          if (item.id === tab) {
            let paramArray = [];
            let paramObject = {};
            paramObject.name = "组合代码";
            paramObject.value = item.id;
            paramObject.displayValue = item.name;
            paramArray.push(paramObject);
            paramObject = {};
            paramObject.name = "估值日期";
            paramObject.value = this.listQuery.valDate;
            paramObject.displayValue = this.listQuery.valDate;
            paramArray.push(paramObject);
            item.content = JSON.stringify(paramArray) //paramArray.toString();
            this.addTab(item);
          }
        });
      });
      this.timer = new Date().getTime();
    },
    addTab(tabObj) {
      //console.log(tabObj);
      this.fundTabs.push({
        title: tabObj.name,
        name: tabObj.id,
        content: tabObj.content,
      });
      this.fundTabsValue = tabObj.id;
    },
    removeTab(targetName) {
      let tabs = this.fundTabs;
      let activeName = this.fundTabsValue;
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            let nextTab = tabs[index + 1] || tabs[index - 1];
            if (nextTab) {
              activeName = nextTab.name;
            }
          }
        });
      }
      this.fundTabsValue = activeName;
      this.fundTabs = tabs.filter((tab) => tab.name !== targetName);
    },
  },
};
</script>

